package com.ivi.car.module.city.view.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.wm.machine.baselibrary.utils.DensityUtils;

/**
 * 字母列表显示
 */
public class SideBar extends View {
	private final int half = 2; // x坐标等于中间-字符串宽度的一半.
	private final int size = 15; //文字大小
	// 触摸事件
	private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	// 26个字母
	private static String[] b = {"热门", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };
	private  int choose; // 选中
	private Paint paint = new Paint();
	private TextView mTextDialog;
    private Context mContext;
	public void setTextView(TextView mTextDialog) {
		this.mTextDialog = mTextDialog;
	}


	public SideBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		choose = choosed;
	}

	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		choose = choosed;
	}

	public SideBar(Context context) {
		super(context);
		this.mContext = context;
		choose = choosed;
	}

	/**
	 *
	 * @param canvas 画布
	 */
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 获取焦点改变背景颜色.
		int height = getHeight(); // 获取对应高度
		int width = getWidth(); // 获取对应宽度
		int singleHeight = height / b.length; // 获取每一个字母的高度

		for (int i = 0; i < b.length; i++) {
			paint.setColor(Color.GRAY);
			// paint.setColor(Color.WHITE);
			paint.setAntiAlias(true);
			paint.setTextSize(DensityUtils.dp2px(mContext, size));
			// 选中的状态
			if (i == choose) {
				paint.setColor(Color.parseColor("#3399ff"));
				paint.setFakeBoldText(true);
			}

			float xPos = width / half - paint.measureText(b[i]) / half;
			float yPos = singleHeight * i + singleHeight;
			canvas.drawText(b[i], xPos, yPos, paint);
			paint.reset(); // 重置画笔
		}

	}
	private  final int choosed = -1;
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY(); // 点击y坐标
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		final int c = (int) (y / getHeight() * b.length); // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
		switch (action) {
		case MotionEvent.ACTION_UP:
			setBackground(new ColorDrawable(0x00000000));
			choose = choosed;
			invalidate();
			if (mTextDialog != null) {
				mTextDialog.setVisibility(View.INVISIBLE);
			}
			break;

		default:
			if (oldChoose != c) {
				if (c >= 0 && c < b.length) {
					if (listener != null) {
						listener.onTouchingLetterChanged(b[c]);
					}
					if (mTextDialog != null) {
						mTextDialog.setText(b[c]);
						mTextDialog.setVisibility(View.VISIBLE);
					}
					
					choose = c;
					invalidate();
				}
			}

			break;
		}
		return true;
	}

	/**
	 * 向外公开的方法
	 * @param onTouchingLetterChangedListener 回调接口
	 */
	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	/**
	 * 接口
	 * 
	 * @author coder
	 * 
	 */
	public interface OnTouchingLetterChangedListener {
		/**
		 * @param s 点击的字母
		 */
		 void onTouchingLetterChanged(String s);
	}
}
